package com.example.pcdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.format.Time;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.rfid.api.MonitorLogHelper;
import com.rfid.api.PCReaderInterface;
import com.rfid.api.BluetoothCfg;
import com.rfid.api.GFunction;
import com.rfid.api.ISO14443AInterface;
import com.rfid.api.ISO14443ATag;
import com.rfid.api.ISO15693Interface;
import com.rfid.api.ISO15693Tag;
import com.rfid.def.ApiErrDefinition;
import com.rfid.def.RfidDef;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

public class MainActivity  extends Activity implements View.OnClickListener {
    private static final int INVENTORY_MSG = 1;
    private static final int GETSCANRECORD = 2;
    private static final int INVENTORY_FAIL_MSG = 4;
    private static final int THREAD_END = 3;
    private TabHost myTabhost = null;
    private Spinner sn_commType = null;// Connector
    private Spinner sn_devName = null;// Device type
    private EditText ed_ipAddr = null;// IP
    private EditText ed_port = null;// Port
    private Spinner sn_bluetooth = null;// bluetooth
    private Spinner sn_comName = null;// com
    private Spinner sn_comBaud = null;
    private Spinner sn_comFrame = null;
    private Button btn_connect = null;// connect tag
    private Button btn_disconnect = null;// disconnect
    private Button btn_getDevInfo = null;// get device information
    private Button btn_setTime = null;// set time
    private Button btn_startInventory = null;// start inventory
    private Button btn_stopInventory = null;// stop inventory
    private Button btn_setInventoryPara = null;// set para of inventory
    private Button btn_startScanf = null;// Start scanf
    private Button btn_stopScanf = null;// stop scanf
    private Button btn_openRF = null;// Open RF
    private Button btn_closeRF = null;// Close RF
    private Button btn_clearInventoryRecord = null;// Clear inventory list
    private Button btn_clearScanfRecordList = null;// clear scanf record
    private Spinner sn_RfPower = null;// RF Power
    private Button btn_readPower = null;// Get RF Power
    private Button btn_setPower = null;// Set RF Power
    private Button btn_loadDefault = null;// Reset
    private Spinner sn_overflow_time = null;// overflow_time
    private Button btn_read_overflow_time = null;// get overflow_time
    private Button btn_write_overflow_time = null;// Set overflow_time

    private ListView list_inventory_record = null;// inventory list
    private ListView list_scanf_record = null;// scanf record list
    private ListView list_tag_name = null;// tag name
    /*
    日志
     */
    private  EditText txtLog=null;
    /*
  搜索
   */
    private  Button btn_findnet=null;
    static int INVENTORY_NET_CODE = 2;// requestCode
    /*
    /*
     配置项
     */
    private  Button btn_readconfig=null;
    private  Button btn_writeconfig=null;
    private  Button btn_loadconfig=null;
    private  Button btn_saveconfig=null;
    private  Spinner sn_devAddr=null;
    private  Spinner sn_devBaud=null;
    private  Spinner sn_devCheck=null;
    private  Spinner sn_devWork=null;
    private  Spinner sn_devPower=null;
    private  Spinner sn_devAFI=null;
    private  TextView txt_Afi=null;
    private  Spinner sn_AFIValue=null;
    private  Spinner sn_devRFoff=null;
    private  Spinner sn_devClash=null;
    private  Spinner sn_devAnt=null;
    private  Spinner sn_devNew=null;
    private  Spinner sn_startblock=null;
    private  Spinner sn_blocknum=null;
    private  Spinner sn_devUid=null;
    private  Spinner sn_devBlock=null;
    private  Spinner sn_devData=null;
    private  Spinner sn_SysSplit=null;
    private  Spinner sn_devSplit=null;
    private Spinner sn_devSplitEnd=null;
    private Spinner sn_devSplitEndType=null;
    private  Spinner sn_devFilter=null;
    private  EditText txt_split=null;
    private  EditText txt_splitend=null;
    private ListView list_ant1 = null;
    private ListView list_ant2 = null;
    private ArrayList<HashMap<String, Object>> listAntData1 = new ArrayList<HashMap<String, Object>>();
    private CheckboxAdapter listAntAdapter1 = null;
    private ArrayList<HashMap<String, Object>> listAntData2 = new ArrayList<HashMap<String, Object>>();
    private CheckboxAdapter listAntAdapter2 = null;
   /**********************************************/
    private TextView tv_inventoryInfo = null;
    private TextView tv_scanRecordInfo = null;
    private List<InventoryReport> inventoryList = new ArrayList<InventoryReport>();
    private List<ScanReport> scanfReportList = new ArrayList<ScanReport>();
    private InventoryAdapter inventoryAdapter = null;
    private ScanAdapter scanfAdapter = null;
    static PCReaderInterface m_reader = new PCReaderInterface();
    static int INVENTORY_REQUEST_CODE = 1;// requestCode

    private boolean bOnlyReadNew = false;
    private boolean bMathAFI = false;
    private byte mAFIVal = 0x00;
    private boolean bBuzzer = true;
    private boolean bUseISO15693 = false;
    private boolean bUseISO14443A = false;
    private long mAntCfg = 0x000000;
    private boolean bRealShowTag = false;

    private long mLoopCnt = 0;

    private Thread m_inventoryThrd = null;// The thread of inventory
    private Thread m_getScanRecordThrd = null;// The thead of scanf the record.
    // Only for rpan device.

    private boolean isLoadScanfMode = false;
    //新增注释
    private int[] layRes = { R.id.tab_reader,R.id.tab_config, R.id.tab_command,
            R.id.tab_inventory, R.id.tab_TagTypeList, R.id.tab_ScanRecord };
    private String[] layTittle = null; // { "设备", "命令", "盘点", "读写", "扫描" };
    private byte[ ][ ] m_configs = new byte[21][8];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        layTittle = new String[] { getString(R.string.tx_tab_device),
                getString(R.string.tx_tab_config),
                getString(R.string.tx_tab_command),
                getString(R.string.tx_tab_inventory),
                getString(R.string.tx_tab_operate),
                getString(R.string.tx_tab_scanf) };

        sn_commType = (Spinner) findViewById(R.id.sn_commType);
        sn_devName = (Spinner) findViewById(R.id.sn_devType);
        ed_ipAddr = (EditText) findViewById(R.id.ed_ipAddr);
        ed_port = (EditText) findViewById(R.id.ed_port);
        myTabhost = (TabHost) findViewById(R.id.tabhost);
        list_inventory_record = (ListView) findViewById(R.id.list_inventory_record);
        list_scanf_record = (ListView) findViewById(R.id.list_scanf_record);
        sn_bluetooth = (Spinner) findViewById(R.id.sn_blueName);
        sn_comName = (Spinner) findViewById(R.id.sn_comName);
        sn_comBaud = (Spinner) findViewById(R.id.sn_comBaud);
        sn_comFrame = (Spinner) findViewById(R.id.sn_comFrame);
        btn_connect = (Button) findViewById(R.id.btn_connect);
        btn_disconnect = (Button) findViewById(R.id.btn_disconnect);
        btn_getDevInfo = (Button) findViewById(R.id.btn_getDevInfo);
        btn_setTime = (Button) findViewById(R.id.btn_setTime);
        btn_startInventory = (Button) findViewById(R.id.btn_startInventory);
        btn_stopInventory = (Button) findViewById(R.id.btn_stopInventory);
        btn_setInventoryPara = (Button) findViewById(R.id.btn_paraInventory);
        btn_startScanf = (Button) findViewById(R.id.btn_startScanfRecord);
        btn_stopScanf = (Button) findViewById(R.id.btn_stopScanfRecord);
        btn_openRF = (Button) findViewById(R.id.btn_openRF);
        btn_closeRF = (Button) findViewById(R.id.btn_closeRF);
        sn_RfPower = (Spinner) findViewById(R.id.sn_powerVal);
        btn_readPower = (Button) findViewById(R.id.btn_readPower);
        btn_setPower = (Button) findViewById(R.id.btn_setPower);
        btn_clearInventoryRecord = (Button) findViewById(R.id.btn_clearList);
        btn_clearScanfRecordList = (Button) findViewById(R.id.btn_clearScanfRecordList);
        btn_loadDefault = (Button) findViewById(R.id.btn_loadDefault);
        tv_inventoryInfo = (TextView) findViewById(R.id.tv_inventoryInfo);
        tv_scanRecordInfo = (TextView) findViewById(R.id.tv_scanRecordInfo);
        list_tag_name = (ListView) findViewById(R.id.list_tagName);
        sn_overflow_time = (Spinner) findViewById(R.id.sn_overflow_time);
        btn_read_overflow_time = (Button) findViewById(R.id.btn_read_overflow_time);
        btn_write_overflow_time = (Button) findViewById(R.id.btn_write_overflow_time);
        /*
        日志
         */
        txtLog=(EditText)findViewById(R.id.txtLog);
        /*
         搜索
         */
        btn_findnet=(Button)findViewById(R.id.btn_findnet);
        btn_findnet.setOnClickListener(this);

        /****************************************************/
        btn_readconfig =(Button) findViewById(R.id.btn_readconfig);
        btn_readconfig.setOnClickListener(this);
        btn_writeconfig=(Button)findViewById(R.id.btn_writeconfig);
        btn_writeconfig.setOnClickListener(this);
        btn_loadconfig=(Button)findViewById(R.id.btn_loadconfig);
        btn_loadconfig.setOnClickListener(this);
        btn_saveconfig=(Button)findViewById(R.id.btn_saveconfig);
        btn_saveconfig.setOnClickListener(this);
        sn_devAddr=(Spinner)findViewById(R.id.sn_devAddr);
        sn_devCheck=(Spinner)findViewById(R.id.sn_devCheck);
        sn_devBaud=(Spinner)findViewById(R.id.sn_devBaud);
        sn_devWork=(Spinner)findViewById(R.id.sn_devWork);
        sn_devPower=(Spinner)findViewById(R.id.sn_devPower);
        sn_devAFI=(Spinner)findViewById(R.id.sn_devAFI);
        txt_Afi=(TextView)findViewById(R.id.txt_Afi);
        sn_AFIValue=(Spinner)findViewById(R.id.sn_AFIValue);
        sn_devRFoff=(Spinner)findViewById(R.id.sn_devRFOFF);
        sn_SysSplit=(Spinner)findViewById(R.id.sn_SysSplit);
        sn_devClash=(Spinner)findViewById(R.id.sn_devClash);
        sn_devAnt=(Spinner)findViewById(R.id.sn_devAnt);
        sn_devNew=(Spinner)findViewById(R.id.sn_devNew);
        sn_blocknum=(Spinner)findViewById(R.id.sn_blocknum);
        sn_startblock=(Spinner)findViewById(R.id.sn_startblock);
        sn_devUid=(Spinner)findViewById(R.id.sn_devUid);
        sn_devBlock=(Spinner)findViewById(R.id.sn_devBlock);
        sn_devData=(Spinner)findViewById(R.id.sn_devData);
        sn_devSplit=(Spinner)findViewById(R.id.sn_devSplit);
        sn_devSplitEnd=(Spinner)findViewById(R.id.sn_devSplitEnd);
        sn_devSplitEndType=(Spinner)findViewById(R.id.sn_devSplitEndType);
        sn_devFilter =(Spinner)findViewById(R.id.sn_devFilter);
        txt_split=(EditText)findViewById(R.id.txt_split);
        txt_splitend=(EditText)findViewById(R.id.txt_splitend);
        list_ant1=(ListView)findViewById(R.id.list_ant1);
        listAntAdapter1 = new CheckboxAdapter(this, listAntData1);
        list_ant1.setAdapter(listAntAdapter1);
        for (int i = 0; i < 30; i++)
        {
            int antNum = i+1;
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ant_name", "天线#" + antNum);
            if ((mAntCfg&(1<<i))!=0)
            {
                map.put("selected", true);
            }
            else
            {
                map.put("selected", false);
            }
            listAntData1.add(map);
        }
        listAntAdapter1.notifyDataSetChanged();
        list_ant2=(ListView)findViewById(R.id.list_ant2);
        listAntAdapter2 = new CheckboxAdapter(this, listAntData2);
        list_ant2.setAdapter(listAntAdapter2);
        for (int i = 0; i < 30; i++)
        {
            int antNum = i+1;
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ant_name", "天线#" + antNum);
            if ((mAntCfg&(1<<i))!=0)
            {
                map.put("selected", true);
            }
            else
            {
                map.put("selected", false);
            }
            listAntData2.add(map);
        }
        listAntAdapter2.notifyDataSetChanged();
        /****************************************************/
        sn_commType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id)
            {
                // TODO Auto-generated method stub
                //MainActivity.com
                CommTypeChange();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
                // TODO Auto-generated method stub

            }

        });

        // Load page
        myTabhost.setup();
        for (int i = 0; i < layRes.length - 1; i++)
        {
            TabHost.TabSpec myTab = myTabhost.newTabSpec("tab" + i);
            myTab.setIndicator(layTittle[i]);
            myTab.setContent(layRes[i]);
            myTabhost.addTab(myTab);
        }
        myTabhost.setCurrentTab(0);
        myTabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                if (!tabId.equals("tab3"))
                {
                    b_inventoryThreadRun=false;
                }
            }
        });
        // Inventory list tittle
        ViewGroup InventorytableTitle = (ViewGroup) findViewById(R.id.inventorylist_title);
        InventorytableTitle.setBackgroundColor(Color.rgb(255, 100, 10));

        // Scanf list tittle
        ViewGroup ScanRecordTableTitle = (ViewGroup) findViewById(R.id.scan_record_list_tittle);
        ScanRecordTableTitle.setBackgroundColor(Color.rgb(53, 190, 106));

        scanfAdapter = new ScanAdapter(this, scanfReportList);
        list_scanf_record.setAdapter(scanfAdapter);
        inventoryAdapter = new InventoryAdapter(this, inventoryList);
        list_inventory_record.setAdapter(inventoryAdapter);

        final String[] tagName = new String[] { "ICODE SLI"};// "Mifare S50","Mifare Ultralight" ,"ISO14443A-4 Transceive"};
        List<Map<String, Object>> tagNameListItems = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < tagName.length; i++)
        {
            Map<String, Object> map = new HashMap<String, Object>(); // 实例化Map对象
            map.put("image", R.drawable.arrow);
            map.put("title", tagName[i]);
            tagNameListItems.add(map); // 将map对象添加到List集合中
        }

        // 读写选项标签列表
        @SuppressLint("ResourceType") SimpleAdapter tagNamadapter = new SimpleAdapter(this, tagNameListItems,
                R.xml.tag_name_items, new String[] { "title", "image" },
                new int[] { R.id.tagListtitle, R.id.tagListimage }); // 创建SimpleAdapter
        list_tag_name.setAdapter(tagNamadapter);
        list_tag_name.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3)
            {
                Intent intent = null;
                switch (arg2)
                {
                    case 0:
                        if (!m_reader.PC_IsAirProtocolSupport(RfidDef.RFID_APL_ISO15693_ID))
                        {
                            Toast.makeText(MainActivity.this, "Unsupported",
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                        intent = new Intent(MainActivity.this,
                                IcodesliTagActivity.class);
                        startActivity(intent);
                        String s="";
                        break;
                    case 1:
                        if (!m_reader.PC_IsAirProtocolSupport(RfidDef.RFID_APL_ISO14443A_ID))
                        {
                            Toast.makeText(MainActivity.this, "Unsupported",
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                        intent = new Intent(MainActivity.this,
                                MifareS50TagActivity.class);
                        startActivity(intent);
                        break;
                    case 2:
                        if (!m_reader.PC_IsAirProtocolSupport(RfidDef.RFID_APL_ISO14443A_ID))
                        {
                            Toast.makeText(MainActivity.this, "Unsupported",
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                        intent = new Intent(MainActivity.this,
                                UltralightTagActivity.class);
                        startActivity(intent);
                        break;
                    case 3:
                        if (!m_reader.PC_IsAirProtocolSupport(RfidDef.RFID_APL_ISO14443A_ID))
                        {
                            Toast.makeText(MainActivity.this, "Unsupported",
                                    Toast.LENGTH_SHORT).show();
                            return;
                        }
                        intent = new Intent(MainActivity.this,
                                ISO14443Ap4TransceiveActivity.class);
                        startActivity(intent);
                        break;
                    default:
                        break;
                }
            }
        });

        // 列举已配对的蓝牙设备
        // Get the bluetooth
        ArrayList<CharSequence> m_bluetoolNameList = null;
        ArrayAdapter<CharSequence> m_adaBluetoolName = null;
        m_bluetoolNameList = new ArrayList<CharSequence>();
        ArrayList<BluetoothCfg> m_blueList =PCReaderInterface
                .GetPairBluetooth();
        if (m_blueList != null)
        {
            for (BluetoothCfg bluetoolCfg : m_blueList)
            {
                m_bluetoolNameList.add(bluetoolCfg.GetName());
            }
        }

        m_adaBluetoolName = new ArrayAdapter<CharSequence>(this,
                android.R.layout.simple_spinner_dropdown_item,
                m_bluetoolNameList);
        sn_bluetooth.setAdapter(m_adaBluetoolName);

        // 列举所有串口
        // Get the Serial port
        ArrayList<CharSequence> m_comNameList = new ArrayList<CharSequence>();
        ArrayList<CharSequence> m_comBaudList = new ArrayList<CharSequence>();
        ArrayList<CharSequence> m_comFrameList = new ArrayList<CharSequence>();
        ArrayAdapter<CharSequence> m_adaComName = null;
        ArrayAdapter<CharSequence> m_adaComBaud = null;
        ArrayAdapter<CharSequence> m_adaComFrame = null;

        String m_comList[] = PCReaderInterface.GetSerialPortPath();
        for (String s : m_comList)
        {
            m_comNameList.add(s);
        }

        m_comBaudList.add("9600");
        m_comBaudList.add("38400");
        m_comBaudList.add("57600");
        m_comBaudList.add("115200");

        m_comFrameList.add("8N1");
        m_comFrameList.add("8E1");
        m_comFrameList.add("8O1");

        m_adaComName = new ArrayAdapter<CharSequence>(this,
                android.R.layout.simple_spinner_dropdown_item, m_comNameList);
        m_adaComBaud = new ArrayAdapter<CharSequence>(this,
                android.R.layout.simple_spinner_dropdown_item, m_comBaudList);

        m_adaComFrame = new ArrayAdapter<CharSequence>(this,
                android.R.layout.simple_spinner_dropdown_item, m_comFrameList);

        sn_comName.setAdapter(m_adaComName);
        sn_comBaud.setAdapter(m_adaComBaud);
        sn_comFrame.setAdapter(m_adaComFrame);

        ArrayList<CharSequence> overflowTime = new ArrayList<CharSequence>();
        for (int i = 0; i < 256; i++)
        {
            overflowTime.add(i + "");
        }
        ArrayAdapter<CharSequence> overflowTimeAda = new ArrayAdapter<CharSequence>(
                this, android.R.layout.simple_spinner_dropdown_item,
                overflowTime);
        sn_overflow_time.setAdapter(overflowTimeAda);

        btn_connect.setOnClickListener(this);
        btn_disconnect.setOnClickListener(this);
        btn_getDevInfo.setOnClickListener(this);
        btn_setTime.setOnClickListener(this);
        btn_openRF.setOnClickListener(this);
        btn_closeRF.setOnClickListener(this);
        btn_startInventory.setOnClickListener(this);
        btn_stopInventory.setOnClickListener(this);
        btn_clearInventoryRecord.setOnClickListener(this);
        btn_startScanf.setOnClickListener(this);
        btn_stopScanf.setOnClickListener(this);
        btn_clearScanfRecordList.setOnClickListener(this);
        btn_setPower.setOnClickListener(this);
        btn_readPower.setOnClickListener(this);
        btn_loadDefault.setOnClickListener(this);
        btn_setInventoryPara.setOnClickListener(this);
        btn_read_overflow_time.setOnClickListener(this);
        btn_write_overflow_time.setOnClickListener(this);

        btn_connect.setEnabled(true);
        btn_disconnect.setEnabled(false);
        btn_getDevInfo.setEnabled(false);
        btn_setTime.setEnabled(false);
        btn_openRF.setEnabled(false);
        btn_closeRF.setEnabled(false);
        btn_startInventory.setEnabled(false);
        btn_stopInventory.setEnabled(false);
        btn_setInventoryPara.setEnabled(false);
        btn_startScanf.setEnabled(false);
        btn_stopScanf.setEnabled(false);
        sn_RfPower.setEnabled(false);
        btn_readPower.setEnabled(false);
        btn_setPower.setEnabled(false);
        btn_loadDefault.setEnabled(false);
        list_tag_name.setEnabled(false);
        sn_overflow_time.setEnabled(false);
        btn_write_overflow_time.setEnabled(false);
        btn_read_overflow_time.setEnabled(false);

        LoadActivityByHistory();

        CommTypeChange();
        MonitorLogHelper.Log().SetLogEvent(new MonitorLogHelper.LogObserver() {
            @Override
            public void Clear() {
                mLogHandler.sendEmptyMessage(0);
                //txtLog.setText("");
            }

            @Override
            public void Log(String o) {
                Message msg=new Message();
                msg.what=1;
                msg.obj=o;
                mLogHandler.sendMessage(msg);
                //String l=txtLog.getText().toString()+o+"\r\n";
               // txtLog.setText(l);
            }
        });
        MonitorLogHelper.Log().Clear();
    }

    private void CommTypeChange()
    {
        LinearLayout bluetoothView = (LinearLayout) findViewById(R.id.group_bluetooth);
        LinearLayout netView = (LinearLayout) findViewById(R.id.group_net);
        RelativeLayout comView = (RelativeLayout) findViewById(R.id.group_com);
        Button FindNet=(Button)findViewById(R.id.btn_findnet);
        switch (sn_commType.getSelectedItemPosition())
        {
            case 0:
                bluetoothView.setVisibility(View.GONE);
                netView.setVisibility(View.GONE);
                FindNet.setVisibility(View.GONE);
                comView.setVisibility(View.VISIBLE);
                break;
            case 1:
                bluetoothView.setVisibility(View.GONE);
                netView.setVisibility(View.VISIBLE);
                FindNet.setVisibility(View.VISIBLE);
                comView.setVisibility(View.GONE);
                break;
            case 2:
                bluetoothView.setVisibility(View.GONE);
                netView.setVisibility(View.GONE);
                comView.setVisibility(View.GONE);
                FindNet.setVisibility(View.GONE);
                break;
        }
        /*
        switch (sn_commType.getSelectedItemPosition())
        {
            case 0:
                bluetoothView.setVisibility(View.VISIBLE);
                netView.setVisibility(View.GONE);
                comView.setVisibility(View.GONE);
                break;
            case 1:
                bluetoothView.setVisibility(View.GONE);
                netView.setVisibility(View.GONE);
                comView.setVisibility(View.VISIBLE);
                break;
            case 2:
                bluetoothView.setVisibility(View.GONE);
                netView.setVisibility(View.VISIBLE);
                comView.setVisibility(View.GONE);
                break;
            case 3:
                bluetoothView.setVisibility(View.GONE);
                netView.setVisibility(View.GONE);
                comView.setVisibility(View.GONE);
                break;
            case 4:
                bluetoothView.setVisibility(View.GONE);
                netView.setVisibility(View.GONE);
                comView.setVisibility(View.VISIBLE);
                break;
            default:
                bluetoothView.setVisibility(View.GONE);
                netView.setVisibility(View.GONE);
                comView.setVisibility(View.GONE);
                break;
        }*/
    }
    @Override
    protected void onDestroy()
    {
        // soundPool.unload(soundID);
        if (m_reader.IsReaderOpen())
        {
            // 如果盘点标签线程正在运行，则关闭该线程
            // If thread of inventory is running,stop the thread before exit the
            // application.
            if (m_inventoryThrd != null && m_inventoryThrd.isAlive())
            {
                b_inventoryThreadRun = false;
                m_reader.PC_SetCommuImmeTimeout();
                try
                {
                    m_inventoryThrd.join();
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }

            // 如果获取扫描记录线程正在运行，则关闭该线程
            // If thread of scannig is running,stop the thread before exit the
            // application.
            /*
            if (m_getScanRecordThrd != null && m_getScanRecordThrd.isAlive())
            {
                bGetScanRecordFlg = false;
                m_reader.PC_SetCommuImmeTimeout();
                try
                {
                    m_getScanRecordThrd.join();
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }*/
            CloseDev();
        }

        super.onDestroy();
    }

    public void onClick(View v)
    {
        MonitorLogHelper.Log().Clear();
        int nret = -1;
        String str = "";
        switch (v.getId())
        {
            case R.id.btn_connect:// connect the device
                OpenDev();
                break;
            case R.id.btn_disconnect:// disconnect the device
                CloseDev();
                break;
            case R.id.btn_getDevInfo:// get the informatin of the device
                GetInformation();
                break;
            case R.id.btn_setTime:
                SetSysTime();
                break;
            case R.id.btn_openRF:// Open RF
                nret = m_reader.PC_OpenRFTransmitter((byte) 1);
                if (nret == ApiErrDefinition.NO_ERROR)
                {
                    str = getString(R.string.tx_openRF_ok);
                }
                else
                {
                    str = getString(R.string.tx_openRF_fail);
                }
                MessageBox(getString(R.string.tx_openRF), str);
                break;
            case R.id.btn_setPower:// Set the power
                byte powerIndex = (byte) (sn_RfPower.getSelectedItemPosition() + 1);
                nret = m_reader.PC_SetRFPower(powerIndex);
                if (nret == ApiErrDefinition.NO_ERROR)
                {
                    str = getString(R.string.tx_setPower_ok);
                }
                else
                {
                    str = getString(R.string.tx_setPower_fail);
                }
                MessageBox(getString(R.string.tx_setPower), str);
                break;
            case R.id.btn_loadDefault:// reset
                nret = m_reader.PC_LoadFactoryDefault();
                if (nret == ApiErrDefinition.NO_ERROR)
                {
                    str = getString(R.string.tx_loadDefault_ok);
                }
                else
                {
                    str = getString(R.string.tx_loadDefault_fail);
                }
                MessageBox(getString(R.string.tx_loadDefault), str);
                break;
            case R.id.btn_readPower:// Get RF Power
                Byte mPower = new Byte((byte) 0);
                nret = m_reader.PC_GetRFPower(mPower);
                if (nret != ApiErrDefinition.NO_ERROR)
                {
                    MessageBox(getString(R.string.tx_getRFPower),
                            getString(R.string.tx_getRFPower_fail) + nret);
                    break;
                }
                sn_RfPower.setSelection(mPower.byteValue() - 1);
                MessageBox(getString(R.string.tx_getRFPower),
                        getString(R.string.tx_getRFPower_ok));
                break;
            case R.id.btn_closeRF:// 关闭射频
                nret = m_reader.PC_CloseRFTransmitter();
                if (nret == ApiErrDefinition.NO_ERROR)
                {
                    str = getString(R.string.tx_CloseRF_ok);// "关闭射频成功！";
                }
                else
                {
                    str = getString(R.string.tx_CloseRF_fail);// "关闭射频失败";
                }
                MessageBox(getString(R.string.tx_CloseRF), str);
                break;
            case R.id.btn_startInventory:// 开始盘点
                btn_connect.setEnabled(false);
                btn_disconnect.setEnabled(false);
                btn_getDevInfo.setEnabled(false);
                btn_setTime.setEnabled(false);
                btn_openRF.setEnabled(false);
                btn_closeRF.setEnabled(false);
                btn_startInventory.setEnabled(false);
                btn_stopInventory.setEnabled(true);
                btn_setInventoryPara.setEnabled(false);
                btn_clearInventoryRecord.setEnabled(false);
                btn_startScanf.setEnabled(false);
                btn_stopScanf.setEnabled(false);
                sn_RfPower.setEnabled(false);
                btn_readPower.setEnabled(false);
                btn_setPower.setEnabled(false);
                btn_loadDefault.setEnabled(false);
                list_tag_name.setEnabled(false);
                sn_overflow_time.setEnabled(false);
                btn_read_overflow_time.setEnabled(false);
                btn_write_overflow_time.setEnabled(false);
                inventoryList.clear();
                inventoryAdapter.notifyDataSetChanged();
                tv_inventoryInfo.setText(getString(R.string.tv_inventoryInfo));
                m_inventoryThrd = new Thread(new InventoryThrd());
                m_inventoryThrd.start();
                break;
            case R.id.btn_stopInventory:// 停止盘点
                btn_stopInventory.setEnabled(false);
                m_reader.PC_SetCommuImmeTimeout();
                b_inventoryThreadRun = false;
                break;
            case R.id.btn_paraInventory:// 盘点参数设置
                Intent intent = new Intent(this, InventoryParaActivity.class);
                Bundle bundle = new Bundle();
                bundle.putBoolean("bUseISO15693", this.bUseISO15693);
                bundle.putBoolean("bUseISO14443A", this.bUseISO14443A);
                bundle.putBoolean("OnlyReadNew", this.bOnlyReadNew);
                bundle.putBoolean("MathAFI", this.bMathAFI);
                bundle.putByte("AFI", this.mAFIVal);
                bundle.putBoolean("bBuzzer", this.bBuzzer);
                bundle.putLong("mAntCfg", mAntCfg);
                bundle.putBoolean("bRealShowTag", bRealShowTag);
                intent.putExtras(bundle);
                startActivityForResult(intent, INVENTORY_REQUEST_CODE);
                break;
            case R.id.btn_clearList:// 清空列表
                inventoryList.clear();
                this.inventoryAdapter.notifyDataSetChanged();
                tv_inventoryInfo.setText(getString(R.string.tx_inventory_sum0));
                break;
            case R.id.btn_startScanfRecord:// 开始扫描
                btn_connect.setEnabled(false);
                btn_disconnect.setEnabled(false);
                btn_getDevInfo.setEnabled(false);
                btn_setTime.setEnabled(false);
                btn_openRF.setEnabled(false);
                btn_closeRF.setEnabled(false);
                btn_startInventory.setEnabled(false);
                btn_stopInventory.setEnabled(false);
                btn_setInventoryPara.setEnabled(false);
                btn_clearInventoryRecord.setEnabled(false);
                btn_startScanf.setEnabled(false);
                btn_stopScanf.setEnabled(true);
                btn_clearScanfRecordList.setEnabled(false);
                sn_RfPower.setEnabled(false);
                btn_readPower.setEnabled(false);
                btn_setPower.setEnabled(false);
                btn_loadDefault.setEnabled(false);
                list_tag_name.setEnabled(false);
                sn_overflow_time.setEnabled(false);
                btn_read_overflow_time.setEnabled(false);
                btn_write_overflow_time.setEnabled(false);
                scanfReportList.clear();
                scanfAdapter.notifyDataSetChanged();
                tv_scanRecordInfo.setText(getString(R.string.tx_scanf_sum0));
                //m_getScanRecordThrd = new Thread(new GetScanRecordThrd());
               // m_getScanRecordThrd.start();
                break;
            case R.id.btn_stopScanfRecord:// 停止采集记录
                //btn_stopScanf.setEnabled(false);
                //bGetScanRecordFlg = false;
                break;
            case R.id.btn_clearScanfRecordList:// 清空扫描记录
                scanfReportList.clear();
                tv_scanRecordInfo.setText(getString(R.string.tx_scanf_sum0));
                this.scanfAdapter.notifyDataSetChanged();
                break;
            case R.id.btn_read_overflow_time:// 获取溢出时间
                Integer mTime = 0;
                nret = m_reader.PC_GetOverflowTime(mTime);
                if (nret != ApiErrDefinition.NO_ERROR)
                {
                    MessageBox(getString(R.string.tx_getOverflowTime),
                            getString(R.string.tx_getOverflowTime_fail) + nret);
                    break;
                }
                sn_overflow_time.setSelection(mTime.intValue());
                MessageBox(getString(R.string.tx_getOverflowTime),
                        getString(R.string.tx_getOverflowTime_ok));
                break;
            case R.id.btn_write_overflow_time:// 设置溢出时间
                nret = m_reader.PC_SetOverflowTime(sn_overflow_time
                        .getSelectedItemPosition());
                if (nret != ApiErrDefinition.NO_ERROR)
                {
                    MessageBox(getString(R.string.tx_setOverflowTime),
                            getString(R.string.tx_setOverflowTime_fail) + nret);
                    break;
                }
                MessageBox(getString(R.string.tx_setOverflowTime),
                        getString(R.string.tx_setOverflowTime_ok) + nret);
                break;
            case R.id.btn_readconfig:
                ReadConfig();
                break;
            case R.id.btn_writeconfig:
                WriteConfig();
                break;
            case R.id.btn_saveconfig:
                 SaveConfig();
                break;
            case  R.id.btn_loadconfig:
                nret = m_reader.PC_LoadFactoryDefault();
                if (nret == ApiErrDefinition.NO_ERROR)
                {
                    str = getString(R.string.tx_loadDefault_ok);
                }
                else
                {
                    str = getString(R.string.tx_loadDefault_fail);
                }
                MessageBox(getString(R.string.tx_loadDefault), str);
                break;
            case R.id.btn_findnet:
                Intent intent1 = new Intent(this, NetActivity.class);
                Bundle bundle1 = new Bundle();
                intent1.putExtras(bundle1);
                startActivityForResult(intent1, INVENTORY_NET_CODE);
                break;
            default:
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (data == null)
        {
            return;
        }
        if (requestCode ==INVENTORY_REQUEST_CODE)
        {
            if (resultCode !=InventoryParaActivity.RESULT_OK)
            {
                return;
            }
            Bundle bundle = data.getExtras();
            bUseISO15693 = bundle.getBoolean("bUseISO15693");
            bUseISO14443A = bundle.getBoolean("bUseISO14443A");
            bOnlyReadNew = bundle.getBoolean("OnlyReadNew");
            bMathAFI = bundle.getBoolean("MathAFI");
            mAFIVal = bundle.getByte("AFI");
            bBuzzer = bundle.getBoolean("bBuzzer");
            mAntCfg = bundle.getLong("mAntCfg");
            bRealShowTag = bundle.getBoolean("bRealShowTag");

            saveHistory("AntCfg", mAntCfg);
            saveHistory("bRealShowTag", bRealShowTag);
            super.onActivityResult(requestCode, resultCode, data);
        }
        if (requestCode ==INVENTORY_NET_CODE)
        {
            if (resultCode !=NetActivity.RESULT_OK)
            {
                return;
            }
            Bundle bundle = data.getExtras();
            ed_ipAddr.setText(bundle.getString("IP"));
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    private void MessageBox(String sTittle, String msg)
    {
        new AlertDialog.Builder(this).setTitle(sTittle).setMessage(msg)
                .setPositiveButton(getString(R.string.tx_msg_certain), null)
                .show();
    }
    private  void ReadConfig()
    {
          //读取配置放入16*8的二维数组中
            for(int i=0;i<21;i++)
            {
              byte[] cfgdata=new  byte[8];
              int rt= m_reader.PC_ConfigBlockRead(i,cfgdata);
              if(rt!=0)continue;
              m_configs[i]=cfgdata;
            }
            for (int i=0;i<30;i++)
            {
                listAntData1.get(i).put("selected",false);
                listAntData2.get(i).put("selected",false);
            }
           setListViewHeight(list_ant1);
           listAntAdapter1.notifyDataSetChanged();
           setListViewHeight(list_ant2);
           listAntAdapter2.notifyDataSetChanged();
            //逐个解析数组值
            for(int i=0;i<21;i++)
            {
                byte[] cfgdata=m_configs[i];
                if (i==0)
                {
                   //配置0
                    sn_devAddr.setSelection((cfgdata[0]&0xFF)-1);
                    switch (cfgdata[1]&0xFF)
                    {
                        case 0x96:
                            sn_devBaud.setSelection(0);
                            break;
                        case  0x19:
                            sn_devBaud.setSelection(1);
                            break;
                        case  0x38:
                            sn_devBaud.setSelection(2);
                            break;
                        case  0x57:
                            sn_devBaud.setSelection(3);
                            break;
                        case  0x11:
                            sn_devBaud.setSelection(4);
                            break;
                    }
                    switch (cfgdata[2]&0xFF)
                    {
                        case 0x00:
                            sn_devCheck.setSelection(0);
                            break;
                        case 0x01:
                            sn_devCheck.setSelection(1);
                            break;
                        case 0x02:
                            sn_devCheck.setSelection(2);
                            break;
                    }
                }
                if (i==1)//配置块1
                {
                    /*
                    m_configs[1][1] = (byte) (mWAntCfg & 0xFF);
                    m_configs[1][2] = (byte) (mWAntCfg >> 8 & 0xFF);
                    m_configs[1][3] = (byte) (mWAntCfg >> 16 & 0xFF);
                    m_configs[1][4] = (byte) (mWAntCfg >> 24 & 0xFF);*/
                    int power=cfgdata[0];
                    sn_devPower.setSelection(power-1);
                      long mCfgAnt=(cfgdata[4]&0xFF)<<24|(cfgdata[3]&0xFF)<<16|(cfgdata[2]&0xFF)<<8|(cfgdata[1]&0xFF);
                      Log.d("ReadConfig", "ReadConfig: "+mCfgAnt);
                      for (int n = 0; n < 30; n++)
                      {
                        if ((mCfgAnt & (1 << n)) != 0)
                        {
                            listAntData1.get(n).put("selected",true);
                        }
                      }
                    setListViewHeight(list_ant1);
                      listAntAdapter1.notifyDataSetChanged();
                }
                if (i==2)//配置块2
                {
                    if ((cfgdata[0] & 0x80) !=0x80)
                    {
                       sn_devClash.setSelection(0);
                    }else
                    {
                        sn_devClash.setSelection(1);
                    }

                }
                if (i==3)
                {
                    //打开关闭射频
                    if ((cfgdata[0]&0x04) ==0x04)
                    {
                        sn_devRFoff.setSelection(1);
                    }else {
                        sn_devRFoff.setSelection(0);
                    }
                    //设置是否关闭AFI
                    if ((cfgdata[0]&0x02)!=0x02)
                    {
                        sn_devAFI.setSelection(0);
                    }
                    else
                    {
                        sn_devAFI.setSelection(1);
                    }
                    //AFI值
                    //String afi = Integer.toHexString(cfgdata[1] & 0xFF);
                    int afi=cfgdata[1] & 0xFF;
                    sn_AFIValue.setSelection(afi);
                    //txt_Afi.setText(afi.length()==1?"0"+afi:afi);
                    //上传天线号
                    if ((cfgdata[0]&0x10)!=0x10)
                    {
                        sn_devAnt.setSelection(0);
                    }else
                    {
                        sn_devAnt.setSelection(1);
                    }
                    //只读新标签
                    if ((cfgdata[0]&0x08)!=0x08)
                    {
                        sn_devNew.setSelection(0);
                    }else
                    {
                        sn_devNew.setSelection(1);
                    }
                    int start=cfgdata[3]& 0xFF;
                    sn_startblock.setSelection(start);
                    int blocknum=cfgdata[4]& 0xFF;
                    sn_blocknum.setSelection(blocknum);

                }
                if (i==4)
                {
                    //工作模式
                    if ((cfgdata[0]&0xA0)!=0xA0)
                    {
                        sn_devWork.setSelection(0);
                    }
                    else
                    {
                        sn_devWork.setSelection(1);
                    }
                    if ((cfgdata[1]&0xFF)==0x00)
                    {
                        sn_devFilter.setSelection(0);
                    }else if ((cfgdata[1]&0xFF)==0xFF)
                    {
                        sn_devFilter.setSelection(1);
                    }else {
                        sn_devFilter.setSelection(cfgdata[1]+1);
                    }

                }
                if (i==6)
                {
                    //数据上传格式
                    if ((cfgdata[0]&0x80)!=0x80)
                    {
                        sn_devData.setSelection(0);
                    }else
                    {
                        sn_devData.setSelection(1);
                    }
                    //输出标签UID号
                    if ((cfgdata[0]&0x01)!=0x01)
                    {
                        sn_devUid.setSelection(0);
                    }else
                    {
                        sn_devUid.setSelection(1);
                    }
                    //输出数据块
                    if ((cfgdata[0] &0x02)!=0x02)
                    {
                        sn_devBlock.setSelection(0);
                    }else
                    {
                        sn_devBlock.setSelection(1);
                    }
                    //分隔符类型
                    if ((cfgdata[3]&0x80)!=0x80)
                    {
                        sn_devSplit.setSelection(0);
                    }
                    else
                    {
                        sn_devSplit.setSelection(1);
                    }
                    //分隔符
                    int splitsys=cfgdata[3]&0x0F;
                    sn_SysSplit.setSelection(splitsys);
                    //自定义分割符
                    String split = Integer.toHexString(cfgdata[4] & 0xFF);
                    txt_split.setText(split.length()==1?"0"+split:split);
                    if ((cfgdata[5]&0x80)!=0x80)
                    {
                        sn_devSplitEndType.setSelection(0);
                    }else {
                        sn_devSplitEndType.setSelection(1);
                    }
                    //系统结束符
                    int splitend=cfgdata[5]&0x0F;
                    sn_devSplitEnd.setSelection(splitend);
                    //自定义分割符
                    String  split_end= Integer.toHexString(cfgdata[6] & 0xFF);
                    txt_splitend.setText(split_end.length()==1?"0"+split_end:split_end);

                }
                if (i==7)
                {
                    long mCfgAnt=(cfgdata[3]&0xFF)<<24|(cfgdata[2]&0xFF)<<16|(cfgdata[1]&0xFF)<<8|(cfgdata[0]&0xFF);
                    Log.d("ReadConfig", "ReadConfig: "+mCfgAnt);
                    for (int n = 0; n < 16; n++)
                    {
                        if ((mCfgAnt & (1 << n)) != 0)
                        {
                            listAntData2.get(n).put("selected",true);
                        }
                    }
                    setListViewHeight(list_ant2);
                    listAntAdapter2.notifyDataSetChanged();
                }

            }
        Toast.makeText(this, "读取成功!",
                Toast.LENGTH_SHORT).show();
    }
    private  void WriteConfig()
    {
        for (int i=0;i<16;i++)
        {
            if (i==0)
            {
               int addr=sn_devAddr.getSelectedItemPosition()+1;
               int baud= sn_devBaud.getSelectedItemPosition();

               byte mbaud=0x00;
               switch (baud)
               {
                   case  0:
                       mbaud=(byte)0x96;
                       break;
                   case  1:
                       mbaud=(byte)0x19;
                       break;
                   case  2:
                       mbaud=(byte)0x38;
                       break;
                   case  3:
                       mbaud=(byte)0x57;
                       break;
                   case  4:
                       mbaud=(byte)0x11;
                       break;
               }
               int check=sn_devCheck.getSelectedItemPosition();
               m_configs[0][0]=(byte)addr;
               m_configs[0][1]=mbaud;
               m_configs[0][2]=(byte)check;
            }
            if (i==1)
            {
                //设定功率
               int power=  sn_devPower.getSelectedItemPosition()+1;
               m_configs[1][0]=(byte)power;
                long mWAntCfg = 0x00000000;
                HashMap<Integer, Boolean> state = listAntAdapter1.state;
                for (int j = 0; j < 30; j++)
                {
                    if (state.get(j) != null && state.get(j))
                    {
                        mWAntCfg|=(1<<j);
                    }
                }
                m_configs[1][1] = (byte) (mWAntCfg & 0xFF);
                m_configs[1][2] = (byte) (mWAntCfg >> 8 & 0xFF);
                m_configs[1][3] = (byte) (mWAntCfg >> 16 & 0xFF);
                m_configs[1][4] = (byte) (mWAntCfg >> 24 & 0xFF);
            }
            if (i==2)
            {
                int m_cash=sn_devClash.getSelectedItemPosition();
                if(m_cash==0)
                {
                    m_configs[2][0] =(byte)(m_configs[2][0]&0x7f);
                }else
                {
                    m_configs[2][0]=(byte)(m_configs[2][0]|0x80);
                }

            }
            if (i==3)
            {
                //打开关闭射频
                int devoff=sn_devRFoff.getSelectedItemPosition();
                if (devoff==0)
                {
                    m_configs[3][0]=(byte)(m_configs[3][0]&0xfb);
                }else
                {
                    m_configs[3][0]=(byte)(m_configs[3][0]|0x04);
                }
                //是否启用AFI
               int afi=sn_devAFI.getSelectedItemPosition();
                if(afi==0)
                {
                    m_configs[3][0]=(byte)(m_configs[3][0]&0xfd);
                }
                else
                {
                    m_configs[3][0]=(byte)(m_configs[3][0]|0x02);
                }
               //上传天线号
               int ant=sn_devAnt.getSelectedItemPosition();
                if(ant==0)
                {
                    m_configs[3][0]=(byte)(m_configs[3][0]&0xef);
                }
                else
                {
                    m_configs[3][0]=(byte)(m_configs[3][0]|0x10);
                }
               //上传新标签
               int devNew= sn_devNew.getSelectedItemPosition();
                if(devNew==0)
                {
                    m_configs[3][0]=(byte)(m_configs[3][0]&0xf7);
                }
                else
                {
                    m_configs[3][0]=(byte)(m_configs[3][0]|0x08);
                }
                //AFI值
                int m_afi= sn_AFIValue.getSelectedItemPosition();;
               //String strAFT=txt_Afi.getText().toString();
               // byte[] afis=GFunction.decodeHex(strAFT);
                m_configs[3][1]=(byte)m_afi;
                //起始块
                int start= sn_startblock.getSelectedItemPosition();
                m_configs[3][3]=(byte)start;
                //块数量
                int blocknum= sn_blocknum.getSelectedItemPosition();
                m_configs[3][4]=(byte)blocknum;
            }
            if (i==4)
            {
                int work=sn_devWork.getSelectedItemPosition();
                if(work==0)
                {
                    m_configs[4][0]=0;
                }
                else
                {
                    m_configs[4][0]=(byte)0xa0;
                }
                int fliter=sn_devFilter.getSelectedItemPosition();
                if(fliter==0)
                {
                    m_configs[4][1]=0;
                }
                else if(fliter==1)
                {
                    m_configs[4][1]=(byte)0xff;
                }
                else
                {
                    m_configs[4][1]=(byte)(fliter-1);
                }
            }
            if (i==6)
            {
                //数据上传模式
               int datatype=sn_devData.getSelectedItemPosition();
                if(datatype==0)
                {//否
                    m_configs[6][0]=(byte)(m_configs[6][0]&0xfd);
                }
                else
                {
                    m_configs[6][0]=(byte)(m_configs[6][0]|0x80);
                }
                //输出标签UID号
                int dataUid=  sn_devUid.getSelectedItemPosition();
                if(dataUid==0)
                {//否
                    m_configs[6][0]=(byte)(m_configs[6][0]&0xfe);
                }
                else
                {
                    m_configs[6][0]=(byte)(m_configs[6][0]|0x01);
                }
                //输出数据块
                int dataBlock= sn_devBlock.getSelectedItemPosition();
                if(dataBlock==0)
                {//否
                    m_configs[6][0]=(byte)(m_configs[6][0]&0x7f);
                }
                else
                {
                    m_configs[6][0]=(byte)(m_configs[6][0]|0x02);
                }
                //分隔符类型
                int dataSplit=sn_devSplit.getSelectedItemPosition();
                if(dataSplit==0)
                {//系统值
                    m_configs[6][3]=(byte)(m_configs[6][3]&0x7f);
                }
                else
                {
                    m_configs[6][3]=(byte)(m_configs[6][3]|0x80);
                }

                //分隔符
                int splitsys=sn_SysSplit.getSelectedItemPosition();
                m_configs[6][3]=(byte)(m_configs[6][3]&0xf0);
                m_configs[6][3]=(byte)(m_configs[6][3]|splitsys);

                //自定义分割符
                String strSpilt= txt_split.getText().toString();
                byte[] spilts=GFunction.decodeHex(strSpilt);
                m_configs[6][4] =spilts[0];

                int spiltendtype=sn_devSplitEndType.getSelectedItemPosition();
                if(spiltendtype==0)
                {//系统值
                    m_configs[6][5]=(byte)(m_configs[6][5]&0x7f);
                }
                else
                {
                    m_configs[6][5]=(byte)(m_configs[6][5]|0x80);
                }

                int splitend=  sn_devSplitEnd.getSelectedItemPosition();//cfgdata[5]>>4;
                m_configs[6][5]=(byte)(m_configs[6][5]&0xf0);
                m_configs[6][5]=(byte)(m_configs[6][5]|splitend);
                //自定义分割符
                String  split_end=txt_splitend.getText().toString();// Integer.toHexString(cfgdata[6] & 0xFF);
                byte[]  splitends=GFunction.decodeHex(split_end);
                m_configs[6][6]=splitends[0];
            }
            if (i==7)
            {
                long mWAntCfg = 0x00000000;
                HashMap<Integer, Boolean> state = listAntAdapter2.state;
                for (int j = 0; j < 16; j++)
                {
                    if (state.get(j) != null && state.get(j))
                    {
                        mWAntCfg|=(1<<j);
                    }
                }
                m_configs[i][0] = (byte) (mWAntCfg & 0xFF);
                m_configs[i][1] = (byte) (mWAntCfg >> 8 & 0xFF);
                m_configs[i][2] = (byte) (mWAntCfg >> 16 & 0xFF);
                m_configs[i][3] = (byte) (mWAntCfg >> 24 & 0xFF);
            }
        }
        //将m_configs逐个值写入配置中
        for (int i=0;i<16;i++)
        {
            m_reader.PC_ConfigBlockWrite(i,m_configs[i],8,0);
        }
        Toast.makeText(this, "写入成功!",
                Toast.LENGTH_SHORT).show();
    }
    /*
    保存配置
     */
    private  void  SaveConfig()
    {
        for (int i=0;i<16;i++)
        {
            int ret = m_reader.PC_ConfigBlockSave(i);
        }
        Toast.makeText(this, "保存成功1",
                Toast.LENGTH_SHORT).show();
    }
    //为listview动态设置高度（有多少条目就显示多少条目）
    public void setListViewHeight(ListView listView) {
        //获取listView的adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        //listAdapter.getCount()返回数据项的数目
        for (int i = 0,len = listAdapter.getCount(); i < len; i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() *  (listAdapter .getCount() - 1));
        listView.setLayoutParams(params);
    }
    private void OpenDev()
    {
        String conStr = "";
        String devName = "";
        devName = sn_devName.getSelectedItem().toString();
        int mIdx = sn_commType.getSelectedItemPosition();
        if (mIdx ==0)
        {
            if (sn_comName.getAdapter().isEmpty())
            {
                MessageBox(getString(R.string.tx_msg_selectCom),
                        getString(R.string.tx_msg_selectComTip));
                return;
            }
            conStr = String
                    .format("RDType=%s;CommType=COM;ComPath=%s;Baund=%s;Frame=%s;Addr=255",
                            devName, sn_comName.getSelectedItem().toString(),
                            sn_comBaud.getSelectedItem().toString(),
                            sn_comFrame.getSelectedItem().toString());
        }
        else if (mIdx==1)
        {
            String sRemoteIp = ed_ipAddr.getText().toString();
            String sRemotePort = ed_port.getText().toString();
            conStr = String.format(
                    "RDType=%s;CommType=NET;RemoteIp=%s;RemotePort=%s",
                    devName, sRemoteIp, sRemotePort);
        }
        else if (mIdx==2)
        {
            // 注意：使用USB方式时，必须先要枚举所有USB设备
            // Note: Before using USB, you must enumerate all USB devices first.
            int usbCnt = PCReaderInterface.EnumerateUsb(this);
            if (usbCnt <= 0)
            {
                Toast.makeText(this, getString(R.string.tx_msg_noUsb),
                        Toast.LENGTH_SHORT).show();
                return;
            }

            if (!PCReaderInterface.HasUsbPermission("0"))
            {
                Toast.makeText(this,
                        getString(R.string.tx_msg_noUsbPermission),
                        Toast.LENGTH_SHORT).show();
                PCReaderInterface.RequestUsbPermission("0");
                return;
            }
            conStr = String.format("RDType=%s;CommType=USB;Description=0",
                    devName);
        }
        /*
        if (mIdx == 0)
        {
            if (sn_bluetooth.getAdapter().isEmpty())
            {
                MessageBox(getString(R.string.tx_select_device),
                        getString(R.string.tx_select_bluetooth));
                return;
            }
            String bluetoolName = sn_bluetooth.getSelectedItem().toString();
            if (bluetoolName == "")
            {
                MessageBox(getString(R.string.tx_select_device),
                        getString(R.string.tx_select_bluetooth));
                return;
            }
            conStr = String.format("RDType=%s;CommType=BLUETOOTH;Name=%s",
                    devName, bluetoolName);
        }
        else if (mIdx == 1)// 串口
        {
            if (sn_comName.getAdapter().isEmpty())
            {
                MessageBox(getString(R.string.tx_msg_selectCom),
                        getString(R.string.tx_msg_selectComTip));
                return;
            }
            conStr = String
                    .format("RDType=%s;CommType=COM;ComPath=%s;Baund=%s;Frame=%s;Addr=255",
                            devName, sn_comName.getSelectedItem().toString(),
                            sn_comBaud.getSelectedItem().toString(),
                            sn_comFrame.getSelectedItem().toString());
        }
        else if (mIdx == 2)// (commTypeStr.equals(getString(R.string.tx_type_net)))//
        // 网络
        {
            String sRemoteIp = ed_ipAddr.getText().toString();
            String sRemotePort = ed_port.getText().toString();
            conStr = String.format(
                    "RDType=%s;CommType=NET;RemoteIp=%s;RemotePort=%s",
                    devName, sRemoteIp, sRemotePort);
        }
        else if (mIdx == 3)// (commTypeStr.equals("USB"))
        {
            // 注意：使用USB方式时，必须先要枚举所有USB设备
            // Note: Before using USB, you must enumerate all USB devices first.
            int usbCnt = PCReaderInterface.EnumerateUsb(this);
            if (usbCnt <= 0)
            {
                Toast.makeText(this, getString(R.string.tx_msg_noUsb),
                        Toast.LENGTH_SHORT).show();
                return;
            }

            if (!PCReaderInterface.HasUsbPermission("0"))
            {
                Toast.makeText(this,
                        getString(R.string.tx_msg_noUsbPermission),
                        Toast.LENGTH_SHORT).show();
               PCReaderInterface.RequestUsbPermission("0");
                return;
            }
            conStr = String.format("RDType=%s;CommType=USB;Description=0",
                    devName);
        }
        else if (mIdx == 4)// (commTypeStr.equals(getString(R.string.tx_type_usb_com)))
        {
            // Attention: Only support Z-TEK
            // 注意：目录只支持Z-TEK型号的USB转串口线
            int mUsbCnt = PCReaderInterface.EnumerateZTEK(this, 0x0403, 0x6001);
            if (mUsbCnt <= 0)
            {
                Toast.makeText(this, getString(R.string.tx_msg_noUsb),
                        Toast.LENGTH_SHORT).show();
                return;
            }
            conStr = String
                    .format("RDType=%s;CommType=Z-TEK;port=1;Baund=%s;Frame=%s;Addr=255",
                            devName, sn_comBaud.getSelectedItem().toString(),
                            sn_comFrame.getSelectedItem().toString());

        }
        else
        {
            return;
        }*/
        int iret = m_reader.PC_Open(conStr);
        if (iret == ApiErrDefinition.NO_ERROR)
        {
            // ///////////////////////只有RPAN设备支持扫描模式/////////////////////////////
            if (!isLoadScanfMode && devName.equals("RPAN"))
            {
                findViewById(layRes[4]).setVisibility(View.VISIBLE);
                TabHost.TabSpec myTab = myTabhost.newTabSpec("tab" + 4);
                myTab.setIndicator(layTittle[4]);
                myTab.setContent(layRes[4]);
                myTabhost.addTab(myTab);
                isLoadScanfMode = true;
            }
            ReadConfig();
            // ///////////////////////////////////////////////////
            Toast.makeText(this, getString(R.string.tx_msg_openDev_ok),
                    Toast.LENGTH_SHORT).show();
            SaveActivity();
            mAntCfg = GetHistoryLong("AntCfg");
            bRealShowTag = GetHistoryBool("bRealShowTag");
            sn_devName.setEnabled(false);
            sn_commType.setEnabled(false);
            sn_bluetooth.setEnabled(false);
            ed_ipAddr.setEnabled(false);
            ed_port.setEnabled(false);
            sn_comName.setEnabled(false);
            sn_comBaud.setEnabled(false);
            sn_comFrame.setEnabled(false);
            btn_connect.setEnabled(false);
            btn_disconnect.setEnabled(true);
            btn_getDevInfo.setEnabled(true);
            btn_setTime.setEnabled(true);
            btn_openRF.setEnabled(true);
            btn_closeRF.setEnabled(true);
            btn_startInventory.setEnabled(true);
            btn_stopInventory.setEnabled(false);
            btn_setInventoryPara.setEnabled(true);
            btn_clearInventoryRecord.setEnabled(true);
            btn_startScanf.setEnabled(true);
            btn_stopScanf.setEnabled(false);
            sn_RfPower.setEnabled(true);
            btn_readPower.setEnabled(true);
            btn_setPower.setEnabled(true);
            btn_loadDefault.setEnabled(true);
            list_tag_name.setEnabled(true);
            sn_overflow_time.setEnabled(true);
            btn_write_overflow_time.setEnabled(true);
            btn_read_overflow_time.setEnabled(true);
            sn_overflow_time.setEnabled(true);
        }
        else
        {
            Toast.makeText(this, getString(R.string.tx_msg_openDev_fail),
                    Toast.LENGTH_SHORT).show();
            Toast.makeText(this, conStr,
                    Toast.LENGTH_SHORT).show();
        }
    }
    private void CloseDev()
    {
        if (m_inventoryThrd != null && m_inventoryThrd.isAlive())
        {
            MessageBox("", getString(R.string.tx_msg_stopInventory_tip));
            return;
        }
        if (m_getScanRecordThrd != null && m_getScanRecordThrd.isAlive())
        {
            MessageBox("", getString(R.string.tx_msg_stopScanf_tip));
            return;
        }
        m_reader.PC_Close();
        sn_devName.setEnabled(true);
        sn_commType.setEnabled(true);
        sn_bluetooth.setEnabled(true);
        ed_ipAddr.setEnabled(true);
        ed_port.setEnabled(true);
        sn_comName.setEnabled(true);
        sn_comBaud.setEnabled(true);
        sn_comFrame.setEnabled(true);
        btn_connect.setEnabled(true);
        btn_disconnect.setEnabled(false);
        btn_getDevInfo.setEnabled(false);
        btn_setTime.setEnabled(false);
        btn_openRF.setEnabled(false);
        btn_closeRF.setEnabled(false);
        btn_startInventory.setEnabled(false);
        btn_stopInventory.setEnabled(false);
        btn_setInventoryPara.setEnabled(false);
        btn_startScanf.setEnabled(false);
        btn_stopScanf.setEnabled(false);
        sn_RfPower.setEnabled(false);
        btn_readPower.setEnabled(false);
        btn_setPower.setEnabled(false);
        btn_loadDefault.setEnabled(false);
        list_tag_name.setEnabled(false);
        btn_write_overflow_time.setEnabled(false);
        btn_read_overflow_time.setEnabled(false);
        sn_overflow_time.setEnabled(false);
    }

    private void GetInformation()
    {
        int iret = -1;
        StringBuffer buffer = new StringBuffer();
        iret = m_reader.PC_GetReaderInfo(buffer);
        if (iret == ApiErrDefinition.NO_ERROR)
        {
            new AlertDialog.Builder(this)
                    .setTitle(getString(R.string.tx_msg_getDevInfo))
                    .setMessage(buffer.toString())
                    .setPositiveButton(getString(R.string.tx_msg_certain), null)
                    .show();
        }
        else
        {
            new AlertDialog.Builder(this)
                    .setTitle(getString(R.string.tx_msg_getDevInfo))
                    .setMessage(
                            getString(R.string.tx_msg_getDevInfo_fail) + iret)
                    .setPositiveButton(getString(R.string.tx_msg_certain), null)
                    .show();
        }
    }

    private void SetSysTime()
    {
        int iret = -1;
        Time t = new Time();
        t.setToNow();
        /*
        iret = m_reader.PC_SetSysTime(t.year, t.month, t.monthDay, t.hour,
                t.minute, t.second);
        if (iret == ApiErrDefinition.NO_ERROR)
        {
            new AlertDialog.Builder(this)
                    .setTitle(getString(R.string.tx_msg_setTime))
                    .setMessage(getString(R.string.tx_msg_setTime_ok))
                    .setPositiveButton(getString(R.string.tx_msg_certain), null)
                    .show();
        }
        else
        {
            new AlertDialog.Builder(this)
                    .setTitle(getString(R.string.tx_msg_setTime))
                    .setMessage(getString(R.string.tx_msg_setTime_fail) + iret)
                    .setPositiveButton(getString(R.string.tx_msg_certain), null)
                    .show();
        }*/
    }
    private void FinishInventory()
    {
        btn_connect.setEnabled(false);
        btn_disconnect.setEnabled(true);
        btn_getDevInfo.setEnabled(true);
        btn_setTime.setEnabled(true);
        btn_openRF.setEnabled(true);
        btn_closeRF.setEnabled(true);
        btn_startInventory.setEnabled(true);
        btn_stopInventory.setEnabled(false);
        btn_setInventoryPara.setEnabled(true);
        btn_clearInventoryRecord.setEnabled(true);
        btn_startScanf.setEnabled(true);
        btn_stopScanf.setEnabled(false);
        btn_clearScanfRecordList.setEnabled(true);
        sn_RfPower.setEnabled(true);
        btn_readPower.setEnabled(true);
        btn_setPower.setEnabled(true);
        btn_loadDefault.setEnabled(true);
        list_tag_name.setEnabled(true);
        sn_overflow_time.setEnabled(true);
        btn_read_overflow_time.setEnabled(true);
        btn_write_overflow_time.setEnabled(true);
    }

    private Handler mHandler = new MyHandler(this);
    private static class MyHandler extends Handler
    {
        private final WeakReference<MainActivity> mActivity;

        public MyHandler(MainActivity activity)
        {
            mActivity = new WeakReference<MainActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg)
        {
            MainActivity pt = mActivity.get();
            if (pt == null)
            {
                return;
            }
            boolean b_find = false;
            switch (msg.what)
            {
                case INVENTORY_MSG:// 盘点到标签
                    @SuppressWarnings("unchecked")
                    Vector<Object> tagList = (Vector<Object>) msg.obj;
                    if (pt.bRealShowTag && !pt.inventoryList.isEmpty())
                    {
                        pt.inventoryList.clear();
                    }
                    if (!tagList.isEmpty() && pt.bBuzzer)
                    {
                        VoicePlayer.GetInst(pt).Play();
                    }
                    for (int i = 0; i < tagList.size(); i++)
                    {
                        b_find = false;

                        // ISO15693 TAG
                        if (tagList.get(i) instanceof ISO15693Tag)
                        {
                            ISO15693Tag tagData = (ISO15693Tag) tagList.get(i);
                            String uidStr = GFunction.encodeHexStr(tagData.uid);
                            long mAnt= tagData.ant_id;
                            for (int j = 0; j < pt.inventoryList.size(); j++)
                            {
                                InventoryReport mReport = pt.inventoryList.get(j);
                                if (mReport.getUidStr().equals(uidStr))
                                {
                                    mReport.setFindCnt(mReport.getFindCnt() + 1);
                                    mReport.setFindAnt(mAnt);
                                    b_find = true;
                                    break;
                                }
                            }
                            if (!b_find)
                            {
                                long mCnt = pt.bRealShowTag?0:1;
                                String tagName = ISO15693Interface
                                        .GetTagNameById(tagData.tag_id);

                                pt.inventoryList.add(new InventoryReport(uidStr, tagName,mAnt,mCnt));

                            }
                        }
                        else if (tagList.get(i) instanceof ISO14443ATag)
                        {
                            ISO14443ATag tagData = (ISO14443ATag) tagList.get(i);
                            String uidStr = GFunction.encodeHexStr(tagData.uid);
                            long mAnt= tagData.ant_id;
                            for (int j = 0; j < pt.inventoryList.size(); j++)
                            {
                                InventoryReport mReport = pt.inventoryList.get(j);
                                if (mReport.getUidStr().equals(uidStr))
                                {
                                    mReport.setFindCnt(mReport.getFindCnt() + 1);
                                    mReport.setFindAnt(mAnt);
                                    b_find = true;
                                    break;
                                }
                            }
                            if (!b_find)
                            {
                                long mCnt = pt.bRealShowTag?0:1;
                                String tagName = ISO14443AInterface
                                        .GetTagNameById(tagData.tag_id);

                                pt.inventoryList.add(new InventoryReport(uidStr, tagName,mAnt,mCnt));

                            }
                        }

                    }
                    pt.tv_inventoryInfo.setText(pt
                            .getString(R.string.tx_info_tagCnt)
                            + pt.inventoryList.size()
                            + pt.getString(R.string.tx_info_loopCnt)+ pt.mLoopCnt
                            + pt.getString(R.string.tx_info_failCnt) + msg.arg1
                            + "      时间:"+msg.arg2+"ms");
                    pt.inventoryAdapter.notifyDataSetChanged();
                    break;
                case INVENTORY_FAIL_MSG:
                    pt.tv_inventoryInfo.setText(pt
                            .getString(R.string.tx_info_tagCnt)
                            + pt.inventoryList.size()
                            + pt.getString(R.string.tx_info_loopCnt)+ pt.mLoopCnt
                            + pt.getString(R.string.tx_info_failCnt) + msg.arg1
                            + "      时间:"+msg.arg2+"ms");
                    break;
                case GETSCANRECORD:// 扫描到记录
                    @SuppressWarnings("unchecked")
                    Vector<String> dataList = (Vector<String>) msg.obj;
                    for (String str : dataList)
                    {
                        b_find = false;
                        for (int i = 0; i < pt.scanfReportList.size(); i++)
                        {
                            ScanReport mReport = pt.scanfReportList.get(i);
                            if (str.equals(mReport.getDataStr()))
                            {
                                mReport.setFindCnt(mReport.getFindCnt() + 1);
                                b_find = true;
                            }
                        }
                        if (!b_find)
                        {
                            pt.scanfReportList.add(new ScanReport(str));
                        }

                    }
                    pt.tv_scanRecordInfo.setText(pt
                            .getString(R.string.tx_info_scanfCnt)
                            + pt.scanfReportList.size());
                    pt.scanfAdapter.notifyDataSetChanged();
                    break;
                case THREAD_END:// 线程结束
                    pt.FinishInventory();
                    break;
                default:
                    break;
            }
        }
    }

    private  Handler mLogHandler=new MyLogHandler(this);
    private  static class MyLogHandler extends Handler
    {
        private final WeakReference<MainActivity> mActivity;
        public MyLogHandler(MainActivity activity)
        {
            mActivity = new WeakReference<MainActivity>(activity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            MainActivity pt = mActivity.get();
            if (pt == null)
            {
                return;
            }
            if (msg.what ==1)
            {
                String l=pt.txtLog.getText().toString()+msg.obj.toString()+"\r\n";
                pt.txtLog.setText(l);
            }else
            {
                pt.txtLog.setText("");
            }

           // super.handleMessage(msg);
        }
    }
    private boolean b_inventoryThreadRun = false;
    private class InventoryThrd implements Runnable
    {
        public void run()
        {
            int failedCnt = 0;// 操作失败次数
            Object hInvenParamSpecList = null;
            byte newAI = RfidDef.AI_TYPE_NEW;
            byte useAnt[] = null;
            if (bOnlyReadNew)
            {
                newAI = RfidDef.AI_TYPE_CONTINUE;
            }

            if (mAntCfg != 0)
            {
                Vector<Byte> vAntList = new Vector<Byte>();
                for (int i = 0; i < 32; i++)
                {
                    if ((mAntCfg & (1 << i)) != 0)
                    {
                        vAntList.add((byte) (i + 1));
                    }
                }

                useAnt = new byte[vAntList.size()];
                for (int i = 0; i < useAnt.length; i++)
                {
                    useAnt[i] = vAntList.get(i);
                }
            }

            if (bUseISO14443A || bUseISO15693)
            {
                hInvenParamSpecList = PCReaderInterface.PC_CreateInvenParamSpecList();
                if (bUseISO15693)
                {
                    ISO15693Interface.PC_CreateInvenParam(
                            hInvenParamSpecList, (byte) 0, bMathAFI, mAFIVal,
                            (byte) 0);
                }
                if (bUseISO14443A)
                {
                    ISO14443AInterface.ISO14443A_CreateInvenParam(
                            hInvenParamSpecList, (byte) 0);
                }
            }

            mLoopCnt = 0;
            b_inventoryThreadRun = true;
            while (b_inventoryThreadRun)
            {
                if (mHandler.hasMessages(INVENTORY_MSG))
                {
                    continue;
                }
                MonitorLogHelper.Log().Clear();
                long curTime = System.currentTimeMillis();
                int iret = m_reader.PC_TagInventory(newAI, useAnt, 0, hInvenParamSpecList);
                long curLastTime= System.currentTimeMillis() - curTime;
                if (iret == ApiErrDefinition.NO_ERROR
                        || iret == -ApiErrDefinition.ERR_STOPTRRIGOCUR)
                {
                    Vector<Object> tagList = new Vector<Object>();
                    newAI = RfidDef.AI_TYPE_NEW;
                    if (bOnlyReadNew
                            || iret == -ApiErrDefinition.ERR_STOPTRRIGOCUR)
                    {
                        newAI = RfidDef.AI_TYPE_CONTINUE;
                    }

                    Object tagReport = m_reader.PC_GetTagDataReport(RfidDef.RFID_SEEK_FIRST);
                    while (tagReport != null)
                    {
                        ISO15693Tag ISO15693TagData = new ISO15693Tag();
                        iret = ISO15693Interface.PC_ParseTagDataReport(
                                tagReport, ISO15693TagData);
                        if (iret == ApiErrDefinition.NO_ERROR)
                        {
                            // ISO15693 TAG
                            tagList.add(ISO15693TagData);
                            tagReport = m_reader.PC_GetTagDataReport(RfidDef.RFID_SEEK_NEXT);
                            continue;
                        }
                        /*
                        ISO14443ATag ISO14444ATagData = new ISO14443ATag();
                        iret = ISO14443AInterface.ISO14443A_ParseTagDataReport(
                                tagReport, ISO14444ATagData);
                        if (iret == ApiErrDefinition.NO_ERROR)
                        {
                            // ISO14443A TAG
                            tagList.add(ISO14444ATagData);
                            tagReport = m_reader
                                    .RDR_GetTagDataReport(RfidDef.RFID_SEEK_NEXT);
                            continue;
                        }*/
                    }

                    mLoopCnt++;
                    Message msg = mHandler.obtainMessage();
                    msg.what = INVENTORY_MSG;
                    msg.obj = tagList;
                    msg.arg1 = failedCnt;
                    msg.arg2 =(int) curLastTime;
                    mHandler.sendMessage(msg);
                }
                else
                {
                    mLoopCnt++;
                    newAI = RfidDef.AI_TYPE_NEW;
                    if (b_inventoryThreadRun)
                    {
                        failedCnt++;
                    }
                    Message msg = mHandler.obtainMessage();
                    msg.what = INVENTORY_FAIL_MSG;
                    msg.arg1 = failedCnt;
                    msg.arg2 =(int) curLastTime;
                    mHandler.sendMessage(msg);
                }
            }
            b_inventoryThreadRun = false;
            m_reader.PC_ResetCommuImmeTimeout();
            mHandler.sendEmptyMessage(THREAD_END);// 盘点结束
        }
    };
/*
    private boolean bGetScanRecordFlg = false;
    private class GetScanRecordThrd implements Runnable
    {
        public void run()
        {
            int nret = 0;
            bGetScanRecordFlg = true;
            byte gFlg = 0x00;// 初次采集数据或者上一次采集数据失败时，标志位为0x00
            Object dnhReport = null;
            while (bGetScanRecordFlg)
            {
                if (mHandler.hasMessages(GETSCANRECORD))
                {
                    continue;
                }
                nret = m_reader.RDR_BuffMode_FetchRecords(gFlg);
                if (nret != ApiErrDefinition.NO_ERROR)
                {
                    gFlg = 0x00;
                    continue;
                }
                gFlg = 0x01;
                dnhReport = m_reader
                        .RDR_GetTagDataReport(RfidDef.RFID_SEEK_FIRST);
                Vector<String> dataList = new Vector<String>();
                while (dnhReport != null)
                {
                    String strData = "";
                    byte[] byData = new byte[32];
                    int[] len = new int[1];
                    len[0] = byData.length;
                    if (PCReaderInterface.RDR_ParseTagDataReportRaw(dnhReport, byData,
                            len) == 0)
                    {
                        if (len[0] > 0)
                        {
                            strData = GFunction.encodeHexStr(byData,len[0]);
                            dataList.add(strData);
                        }
                    }
                    dnhReport = m_reader
                            .RDR_GetTagDataReport(RfidDef.RFID_SEEK_NEXT);
                }
                if (!dataList.isEmpty())
                {
                    Message msg = mHandler.obtainMessage();
                    msg.what = GETSCANRECORD;
                    msg.obj = dataList;
                    mHandler.sendMessage(msg);
                }
            }
            bGetScanRecordFlg = false;
            mHandler.sendEmptyMessage(THREAD_END);// 结束
        }
    };
*/
    private void saveHistory(String sKey, String val)
    {
        @SuppressWarnings("deprecation")
        SharedPreferences preferences = this.getSharedPreferences(sKey,
                Context.MODE_PRIVATE );
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(sKey, val);
        editor.commit();
    }

    @SuppressLint("WorldReadableFiles")
    @SuppressWarnings("deprecation")
    private void saveHistory(String sKey, int val)
    {
        SharedPreferences preferences = this.getSharedPreferences(sKey,
                Context.MODE_PRIVATE );
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(sKey, val);
        editor.commit();
    }

    private int GetHistoryInt(String sKey)
    {
        @SuppressWarnings("deprecation")
        @SuppressLint("WorldReadableFiles")
        SharedPreferences preferences = this.getSharedPreferences(sKey,
                Context.MODE_PRIVATE );
        return preferences.getInt(sKey, -1);
    }

    private void saveHistory(String sKey, long val)
    {
        @SuppressWarnings("deprecation")
        SharedPreferences preferences = this.getSharedPreferences(sKey,
                Context.MODE_PRIVATE );
        SharedPreferences.Editor editor = preferences.edit();
        editor.putLong(sKey, val);
        editor.commit();
    }

    private void saveHistory(String sKey, boolean val)
    {
        @SuppressWarnings("deprecation")
        SharedPreferences preferences = this.getSharedPreferences(sKey,
                Context.MODE_PRIVATE );
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(sKey, val);
        editor.commit();
    }

    private boolean GetHistoryBool(String sKey)
    {
        @SuppressWarnings("deprecation")
        @SuppressLint("WorldReadableFiles")
        SharedPreferences preferences = this.getSharedPreferences(sKey,
                Context.MODE_PRIVATE );
        return preferences.getBoolean(sKey, false);
    }

    private long GetHistoryLong(String sKey)
    {
        @SuppressWarnings("deprecation")
        @SuppressLint("WorldReadableFiles")
        SharedPreferences preferences = this.getSharedPreferences(sKey,
                Context.MODE_PRIVATE );
        return preferences.getLong(sKey, 0);
    }

    private String GetHistoryString(String sKey)
    {
        @SuppressWarnings("deprecation")
        @SuppressLint("WorldReadableFiles")
        SharedPreferences preferences = this.getSharedPreferences(sKey,
                Context.MODE_PRIVATE );
        return preferences.getString(sKey, "");
    }

    private void SaveActivity()
    {
        int devItem = 0;
        int commItem = 0;
        int blueToolItem = 0;
        String ipStr = ed_ipAddr.getText().toString();
        String portStr = ed_port.getText().toString();
        int comNameItem = 0;
        int comBaudItem = 0;
        int comFrameItem = 0;

        if (!sn_devName.getAdapter().isEmpty())
        {
            devItem = sn_devName.getSelectedItemPosition();
        }
        if (!sn_commType.getAdapter().isEmpty())
        {
            commItem = sn_commType.getSelectedItemPosition();
        }
        if (!sn_bluetooth.getAdapter().isEmpty())
        {
            blueToolItem = sn_bluetooth.getSelectedItemPosition();
        }

        if (!sn_comName.getAdapter().isEmpty())
        {
            comNameItem = sn_comName.getSelectedItemPosition();
        }
        if (!sn_comBaud.getAdapter().isEmpty())
        {
            comBaudItem = sn_comBaud.getSelectedItemPosition();
        }
        if (!sn_comFrame.getAdapter().isEmpty())
        {
            comFrameItem = sn_comFrame.getSelectedItemPosition();
        }

        saveHistory("DEVNAME", devItem);
        saveHistory("COMMTYPE", commItem);
        saveHistory("COMBAUD", comBaudItem);
        saveHistory("COMFRAME", comFrameItem);
        saveHistory("BLUETOOL", blueToolItem);
        saveHistory("COMNAME", comNameItem);
        saveHistory("DEVIPADDR", ipStr);
        saveHistory("DEVPORT", portStr);
    }

    private void LoadActivityByHistory()
    {
        int devItem = GetHistoryInt("DEVNAME");
        if (devItem < sn_devName.getCount())
        {
            sn_devName.setSelection(devItem);
        }

        int commItem = GetHistoryInt("COMMTYPE");
        if (commItem < sn_commType.getCount())
        {
            sn_commType.setSelection(commItem);
        }

        int blueToolItem = GetHistoryInt("BLUETOOL");
        if (blueToolItem < sn_bluetooth.getCount())
        {
            sn_bluetooth.setSelection(blueToolItem);
        }

        int comNameItem = GetHistoryInt("COMNAME");
        if (comNameItem < sn_comName.getCount())
        {
            sn_comName.setSelection(comNameItem);
        }

        int comBaudItem = GetHistoryInt("COMBAUD");
        if (comBaudItem < sn_comBaud.getCount() && comBaudItem >= 0)
        {
            sn_comBaud.setSelection(comBaudItem);
        }
        else
        {
            sn_comBaud.setSelection(1);
        }

        int comFrameItem = GetHistoryInt("COMFRAME");
        if (comFrameItem < sn_comFrame.getCount() && comFrameItem >= 0)
        {
            sn_comFrame.setSelection(comFrameItem);
        }
        else
        {
            sn_comFrame.setSelection(1);
        }

        String sIp = GetHistoryString("DEVIPADDR");
        if (sIp != "")
        {
            ed_ipAddr.setText(sIp);
        }

        String sPort = GetHistoryString("DEVPORT");
        if (sPort != "")
        {
            ed_port.setText(sPort);
        }
    }



    static public class InventoryAdapter extends BaseAdapter
    {
        private List<InventoryReport> list;
        private LayoutInflater inflater;

        public InventoryAdapter(Context context, List<InventoryReport> list)
        {
            this.list = list;
            inflater = LayoutInflater.from(context);
        }

        public int getCount()
        {
            return list.size();
        }

        public Object getItem(int position)
        {
            return list.get(position);
        }

        public long getItemId(int position)
        {
            return position;
        }

        @SuppressLint("ResourceType")
        public View getView(int position, View convertView, ViewGroup parent)
        {
            InventoryReport inventoryReport = (InventoryReport) this.getItem(position);
            ViewHolder viewHolder;
            if (convertView == null)
            {
                viewHolder = new ViewHolder();
                convertView = inflater
                        .inflate(R.xml.inventorylist_tittle, null);
                viewHolder.mTextUid = (TextView) convertView
                        .findViewById(R.id.tv_inventoryUid);
                viewHolder.mTextTagType = (TextView) convertView
                        .findViewById(R.id.tv_inventoryTagType);
                viewHolder.mTextAnt=(TextView) convertView
                        .findViewById(R.id.tv_inventoryAnt);
                viewHolder.mTextFindCnt = (TextView) convertView
                        .findViewById(R.id.tv_inventoryCnt);
                convertView.setTag(viewHolder);
            }
            else
            {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            long mCnt = inventoryReport.getFindCnt();
            String strCnt = mCnt>0?(mCnt+""):"---";
            String strAnt =inventoryReport.getFindAnt()+"";
            viewHolder.mTextUid.setText(inventoryReport.getUidStr());
            viewHolder.mTextTagType.setText(inventoryReport.getTagTypeStr());
            viewHolder.mTextAnt.setText(strAnt);
            viewHolder.mTextFindCnt.setText(strCnt);

            return convertView;
        }

        private class ViewHolder
        {
            public TextView mTextUid;
            public TextView mTextTagType;
            public  TextView mTextAnt;
            public TextView mTextFindCnt;
        }
    }

    public static class InventoryReport
    {
        private String uidStr;
        private String TagTypeStr;
        private  long  findAnt=0;
        private long findCnt = 0;

        public InventoryReport()
        {
            super();
        }

        public InventoryReport(String uid, String tayType,long ant,long cnt)
        {
            super();
            this.setUidStr(uid);
            this.setTagTypeStr(tayType);
            this.setFindAnt(ant);
            this.setFindCnt(cnt);
        }

        public String getUidStr()
        {
            return uidStr;
        }

        public void setUidStr(String uidStr)
        {
            this.uidStr = uidStr;
        }

        public String getTagTypeStr()
        {
            return TagTypeStr;
        }

        public void setTagTypeStr(String tagTypeStr)
        {
            TagTypeStr = tagTypeStr;
        }

        public void setFindAnt(long ant) {
            findAnt = ant;
        }

        public long getFindAnt() {
            return findAnt;
        }

        public long getFindCnt()
        {
            return findCnt;
        }

        public void setFindCnt(long findCnt)
        {
            this.findCnt = findCnt;
        }
    }

    public static class ScanAdapter extends BaseAdapter
    {
        private List<ScanReport> list;
        private LayoutInflater inflater;

        public ScanAdapter(Context context, List<ScanReport> list)
        {
            this.list = list;
            inflater = LayoutInflater.from(context);
        }

        public int getCount()
        {
            return list.size();
        }

        public Object getItem(int position)
        {
            return list.get(position);
        }

        public long getItemId(int position)
        {
            return position;
        }

        @SuppressLint("ResourceType")
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ScanReport scanfReport = (ScanReport) this.getItem(position);
            ViewHolder viewHolder;
            if (convertView == null)
            {
                viewHolder = new ViewHolder();
                convertView = inflater.inflate(R.xml.scan_record_list_tittle,
                        null);
                viewHolder.mTextData = (TextView) convertView
                        .findViewById(R.id.tv_scanfData);

                viewHolder.mTextFindCnt = (TextView) convertView
                        .findViewById(R.id.tv_scanfCnt);
                convertView.setTag(viewHolder);
            }
            else
            {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.mTextData.setText(scanfReport.getDataStr());
            viewHolder.mTextFindCnt.setText(scanfReport.getFindCnt() + "");
            return convertView;
        }

        public static class ViewHolder
        {
            public TextView mTextData;
            public TextView mTextFindCnt;
        }

    }
    public class CheckboxAdapter extends BaseAdapter
    {
        Context context;
        ArrayList<HashMap<String, Object>> listData;
        @SuppressLint("UseSparseArrays")
        HashMap<Integer, Boolean> state = new HashMap<Integer, Boolean>();

        public CheckboxAdapter(Context context,
                               ArrayList<HashMap<String, Object>> listData)
        {
            this.context = context;
            this.listData = listData;
        }

        public int getCount()
        {
            // TODO Auto-generated method stub
            return listData.size();
        }

        public Object getItem(int position)
        {
            // TODO Auto-generated method stub
            return listData.get(position);
        }

        public long getItemId(int position)
        {
            // TODO Auto-generated method stub
            return position;
        }

        // 重写View
        @SuppressLint("ResourceType")
        public View getView(final int position, View convertView,
                            ViewGroup parent)
        {
            // TODO Auto-generated method stub
            LayoutInflater mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(R.xml.sel_ant_item, null);
            TextView tv_money_name = (TextView) convertView
                    .findViewById(R.id.tv_ant_name);
            tv_money_name.setText((String) listData.get(position).get(
                    "ant_name"));
            CheckBox sel_check = (CheckBox) convertView
                    .findViewById(R.id.sel_ant);
            if ((Boolean) listData.get(position).get("selected"))
            {
                sel_check.setChecked(true);
                state.put(position, true);
            }
            else
            {
                //state.remove(position);
                sel_check.setChecked(false);
                state.put(position, false);
            }
            sel_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
            {
                public void onCheckedChanged(CompoundButton buttonView,
                                             boolean isChecked)
                {

                    if (isChecked)
                    {
                        state.put(position, isChecked);
                        //listData.get(position).put("selected",true);
                    }
                    else
                    {
                        state.remove(position);
                       // listData.get(position).put("selected",false);
                    }
                    //state.put(position, isChecked);
                }
            });
            return convertView;
        }
    }
    static public class ScanReport
    {
        private String dataStr;
        private long findCnt = 0;

        public ScanReport()
        {
            super();
        }

        public ScanReport(String data)
        {
            super();
            this.setDataStr(data);
            this.setFindCnt(1);
        }

        public long getFindCnt()
        {
            return findCnt;
        }

        public void setFindCnt(long findCnt)
        {
            this.findCnt = findCnt;
        }

        public String getDataStr()
        {
            return dataStr;
        }

        public void setDataStr(String dataStr)
        {
            this.dataStr = dataStr;
        }
    }
}
